home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15829 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: snews.tcel.com!netway
  2. From: tech@netway.ab.ca (Ritchie Annand)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Multiple Inheritance Pointer Problem
  5. Date: 8 Apr 1996 10:43:39 GMT
  6. Organization: Telnet Canada (403) 262-5859 info@tcel.com
  7. Message-ID: <4kaqkr$pp3@challenge.tcel.com>
  8. References: <31622A26.3633@nmaa.org>
  9. NNTP-Posting-Host: 204.209.150.53
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. >#include <iostream.h>
  13. >
  14. >class foo
  15. >{
  16. >public:
  17. >    void PrintMe(void) { cerr << "this = " << this << endl; }
  18. >};
  19. >
  20. >class bar
  21. >{
  22. >#ifdef OFF_BY_4
  23. >public:
  24. >    int b;
  25. >#endif
  26. >};
  27. >
  28. >class foobar : public foo, public bar
  29. >{
  30. >#ifdef OFF_BY_4
  31. >public:
  32. >    int fb;
  33. >#endif
  34. >};
  35. >
  36. >class barfoo : public bar, public foo
  37. >{
  38. >#ifdef OFF_BY_4
  39. >public:
  40. >    int bf;
  41. >#endif
  42. >};
  43. >
  44. >main()
  45. >{
  46. >    foobar fb;
  47. >    barfoo bf;
  48. >
  49. >    cerr << "&fb = " << &fb << endl;
  50. >    fb.PrintMe();
  51. >    cerr << "&bf = " << &bf << endl;
  52. >    bf.PrintMe();
  53. >
  54. >    return(0);
  55. >}
  56.  
  57. The problem you're having is that there are some compromises that had to be 
  58. made in the use of pointers with multiple inheritance.
  59.  
  60. PrintMe is a member of the foo class, therefore all objects that call it must 
  61. be from a descendant class of foo.  To be a proper descendant, the class must 
  62. have all of foo's data members in the same order at the same relative 
  63. position as foo.
  64.  
  65. With the foobar class, you have no problems, because the foo part of the 
  66. class comes at the front, just like it would in regular single inheritance.
  67.  
  68. With the barfoo class, in order for barfoo to satisfy the requirements of 
  69. having the data members in the same spot as foo, the this pointer is OFFSET 
  70. when given to foo.  The foo part of the class starts AFTER the bar part of 
  71. the class does.  This occurs even if foo has no data members (as in your 
  72. example)
  73.  
  74. The 2 and 4 offsets you're describing come from the way different compilers 
  75. will handle the ints - some will align the int to a longword (4 bytes), and 
  76. some will keep it as a word (2 byte).  Try putting more data items into bar, 
  77. and you'll see the pointer get more and more offset.
  78.  
  79. There are some things you can do to work around this, but the dirt-simplest 
  80. way is to make sure the object that needs to check its own address it the 
  81. first one in the inheritance list.
  82.  
  83.   --=- Ritchie A.
  84.